package com.spider.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessUtils {
	
	private Matcher match1 = null;
	private Pattern pattern1 = null;
	
	//处理下标
	public String processSub(String cont){
		if(cont.contains("&lt;sub&gt;")){
			String result = "";
			pattern1 = pattern1.compile("&lt;sub&gt;.{0,5}&lt;/sub&gt;");
			match1 = pattern1.matcher(cont);
			if(match1.find()){
				result = cont.replace(match1.group(), "#"+cont.substring(match1.start()+"&lt;sub&gt;".length(),match1.end()-"&lt;/sub&gt;".length()));
				processSub(result);
			}
			return result;
		}
		return cont;
	}

	//处理(上标)方根
	public String processSup(String cont){
		if(cont.contains("&lt;sup&gt;")){
			String result = "";
			pattern1 = pattern1.compile("&lt;sup&gt;.{0,5}&lt;/sup&gt;");
			match1 = pattern1.matcher(cont);
			if(match1.find()){
				result = cont.replace(match1.group(), "^"+cont.substring(match1.start()+"&lt;sup&gt;".length(),match1.end()-"&lt;/sup&gt;".length()));
				processSup(result);
			}
			return result;
		}
		return cont;
	}
	
	//处理(上标)方根
	public static String processSupForJsoup(String cont){
		Matcher match1 = null;
	    Pattern pattern1 = null;
		if(cont.contains("<sup>")){
			String result = "";
			pattern1 = pattern1.compile("<sup>.{0,5}</sup>");
			match1 = pattern1.matcher(cont);
			if(match1.find()){
				result = cont.replace(match1.group(), "^"+cont.substring(match1.start()+"<sup>".length(),match1.end()-"</sup>".length()));
				processSupForJsoup(result);
			}
			return result;
		}
		return cont;
	}
	
	//处理下标
	public static String processSubForJsoup(String cont){
		Matcher match1 = null;
	    Pattern pattern1 = null;
		if(cont.contains("<sub>")){
			String result = "";
			pattern1 = pattern1.compile("<sub>.{0,5}</sub>");
			match1 = pattern1.matcher(cont);
			if(match1.find()){
				result = cont.replace(match1.group(), "#"+cont.substring(match1.start()+"<sub>".length(),match1.end()-"</sub>".length()));
				processSubForJsoup(result);
			}
			return result;
		}
		return cont;
	}
	
	//去除多余的逗号（第一个字符）
	public String processStr(String str){
		String[] strs = str.split("\n");
		StringBuffer buffer = new StringBuffer();
		for (String s : strs) {
			s = s.substring(1, s.length());
			s.trim();
			buffer.append(s+"\n");
		}
		return buffer.toString();
	}
}
